package com.example.newproject_overseas.model.service

import android.annotation.SuppressLint
import android.content.Context
import androidx.datastore.preferences.core.edit
import com.example.newproject_overseas.model.entity.LoginUser
import com.google.gson.Gson
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore


class UserInfoManager(private val context: Context) {

    companion object{
        private val Context.userStore: DataStore<Preferences> by preferencesDataStore("userInfo_store")
        val LocalUserInfo = stringPreferencesKey("LocalUserInfo")
    }


    val loginUser : Flow<String>  = context.userStore.data.map { it[LocalUserInfo] ?: "" }


    @SuppressLint("SuspiciousIndentation")
    suspend fun save(loginUser : LoginUser){

        val loginUserStr = Gson().toJson(loginUser)
        context.userStore.edit {
            it[LocalUserInfo] = loginUserStr;
        }
    }


}